package cn.tedu.shoot;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import java.util.Timer;

public class World extends JPanel {
    private Sky sky;
    private Hero hero;
    private FlyingObject[] enemies = {}; //敌人数组
    private Bullet[] bullets = {}; //子弹数组
    public static final int START = 0;
    public static final int RUNNING = 1;
    public static final int PAUSE = 2;
    public static final int GAME_OVER = 3;
    private int state = START;

    public World(){
        sky = new Sky();
        hero = new Hero();
    }

    @Override
    public void paint(Graphics g) {
        sky.paint(g);
        hero.paint(g);
        for(int i=0;i<enemies.length;i++){
            enemies[i].paint(g);
        }
        for(int i=0;i<bullets.length;i++){
            bullets[i].paint(g);
        }
    }
    public FlyingObject nextOne(){
        Random rand = new Random();
        int type = rand.nextInt(20);
        if(type < 5){
            return new Bee();
        }else if(type < 12){
            return new Airplane();
        }else{
            return new BigAirplane();
        }
    }
    int flyEnterIndex = 0;
    public void enterAction(){
        flyEnterIndex++;
        if(flyEnterIndex%40==0){
            FlyingObject obj = nextOne();
            enemies = Arrays.copyOf(enemies,enemies.length+1);
            enemies[enemies.length-1] = obj;
        }
    }

    int shootIndex = 0;
    public void shootAction(){
        shootIndex++;
        if(shootIndex%30==0){
            Bullet[] bs = hero.shoot();
            bullets = Arrays.copyOf(bullets,bullets.length+bs.length);
            System.arraycopy(bs,0,bullets,bullets.length-bs.length,bs.length);
        }
    }

    public void stepAction(){
        sky.step();
        for(int i=0;i<enemies.length;i++){
            enemies[i].step();
        }
        for(int i=0;i<bullets.length;i++){
            bullets[i].step();
        }
    }
    public void action(){ //启动执行
        MouseAdapter l = new MouseAdapter(){
            public void mouseMoved(MouseEvent e){
                if(state==RUNNING){
                    int x = e.getX();
                    int y = e.getY();
                    hero.moveTo(x, y);
                }
            }
            public void mouseClicked(MouseEvent e){
                switch(state){
                    case START:
                        state = RUNNING;
                        break;
                    case GAME_OVER:
                        hero = new Hero();
                        enemies = new FlyingObject[0];
                        state = START;
                        break;
                }
            }
            public void mouseExited(MouseEvent e){
                if(state==RUNNING){
                    state = PAUSE;
                }
            }
            public void mouseEntered(MouseEvent e){
                if(state==PAUSE){
                    state = RUNNING;
                }
            }
        };
        this.addMouseListener(l);
        this.addMouseMotionListener(l);

        Timer timer = new Timer();
        int intervel = 10;
        timer.schedule(new TimerTask(){
            public void run(){
                if(state==RUNNING){
                    enterAction();
                    stepAction();
                    shootAction();
                }
                repaint();
            }
        },intervel,intervel);

    }
}
